from hep.aida import *

from java.awt import Color
from java.awt import Font
from java.lang import *
from java.util import *
from java.io import *
from java.text import DecimalFormat


from jhplot.jadraw import *
from jhplot  import *

from fr.in2p3.ipnl.aidautilities import *

from jscinttools.datafit import *
from jscinttools.datafit.scint import *
from jscinttools.utilities.aidautilities import *
from jscinttools.utilities.jhplotutilities import *
from jscinttools.utilities.scintutilities import *
from jscinttools.scint.emissionspectrum import *

Locale.setDefault(Locale.ENGLISH)
System.getProperties().setProperty("hep.aida.BatchAnalysisFactory","hep.aida.ref.BatchAnalysisFactory")
af = IAnalysisFactory.create();

showFigures = True
writeFigures = not showFigures

""" Define colors and fonts"""
figType = [".png", ".eps"]
colors = [Color.blue, Color.red, Color(0, 170, 0), Color.black, Color.yellow]
axisLabelFont = Font(Font.SERIF, Font.PLAIN, 25)
titleFont = Font(Font.SERIF, Font.PLAIN, 25)
legendFont = Font(Font.SERIF, Font.PLAIN, 25)
ticFont = Font(Font.SERIF, Font.PLAIN, 20)
statBoxFont = Font(Font.SERIF, Font.PLAIN, 15)

""" Define the Path and files names"""
aidaPath = "/home/marc-antoine/Documents/Dropbox/CryoDAQ"
aidaFile = "ScintillationAnalysis_ZnWO4_Txt"
outPath = aidaPath
corrEmFacPath = "/home/marc-antoine/Documents/Dropbox/CryoDAQ/ZnWO4_EmissionSpec"
corrEmFacFile = "ZnWO4_EmissionSpectra"
win = [1, 1000, 0.95, 1.25]
title = "Light Yield of ZnWO_{4}"
preliminary = True

lyDPSPath = "Light_Yield/LY"
lyCorrDPSPath = "Light_Yield/LY_Corr"
corrEmFacDPSPath = "30eV_Hasylab/corrFacNorm"

lyDPS = AidaFileReader(aidaPath, aidaFile).grabDPS(lyDPSPath)
lyCorrDPS = AidaFileReader(aidaPath, aidaFile).grabDPS(lyCorrDPSPath)
corrEmFacDPS = AidaFileReader(corrEmFacPath, corrEmFacFile).grabDPS(corrEmFacDPSPath)

lyP1D = P1D(lyDPS, title="lyDPS", style="p", color=colors[0], symbol=1, symbolSize=13, errAll=False)
lyCorrP1D = P1D(lyCorrDPS, title="lyCorrDPS", style="p", color=colors[1], symbol=2, symbolSize=13, errAll=False)
lyEmCorrP1D = P1D("lyEmCorrDPS")
lyEmCorrP1D.setStyle("p")
lyEmCorrP1D.setColor(colors[0])
lyEmCorrP1D.setSymbol(2)
lyEmCorrP1D.setSymbolSize(13)
lyEmCorrP1D.setErrAll(False)

emSpecCorrCalc = EmSpecCorrCalculator(corrEmFacDPS)

emCorrValX = []
emCorrValY = []
emCorrValYErr = []
for i in range(lyCorrDPS.size()):
	#print(lyCorrDPS.size())
	#print(i)
	corrFac = emSpecCorrCalc.extrapolCorrFac(lyCorrP1D.getX(i))
	#lyEmCorrP1D.set(i, lyCorrP1D.getX(i), lyCorrP1D.getY(i) / corrFac, lyCorrP1D.getYupper(i) / corrFac,lyCorrP1D.getYlower(i) / corrFac)
	emCorrValX.append(lyCorrP1D.getX(i)) 
	emCorrValY.append(lyCorrP1D.getY(i) / corrFac)
	emCorrValYErr.append(lyCorrP1D.getYupper(i) / corrFac)
	print(corrFac)
	print(lyCorrP1D.getX(i))
	print(lyCorrP1D.getY(i))
	print(emCorrValX[i])
	print(emCorrValY[i])
	print(emCorrValYErr[i])
lyEmCorrP1D.fill(emCorrValX, emCorrValY, emCorrValYErr)	

prelim = JaText("Preliminary", 0.175, 0.2, "NDC", font=titleFont, color=colors[1])
""" Create the Plotter"""
mainPlotter = HPlotJa("LY vs. T", 600, 600, 1, 1, True)
mainPlotter.setGTitle(title, titleFont)
mainPlotter.antiAlias = False
if showFigures:
	mainPlotter.visible(True)
else:
	mainPlotter.visible(False)
		
mainPlotter.setPad(1, 1, 0.125, 0.125, 0.75, 0.75)
mainPlotter.setRange(win[0], win[1], win[2], win[3])
mainPlotter.setLogScale(0, True)
mainPlotter.setLogScale(1, False)
mainPlotter.ticFont = ticFont
mainPlotter.showStatBox = False
mainPlotter.showKey(False)
mainPlotter.nameX = "Temperature (K)"
mainPlotter.nameY = "LY Normalized to 300 K (a.u.)"

#mainPlotter.draw(lyP1D)
mainPlotter.draw(lyCorrP1D)
mainPlotter.draw(lyEmCorrP1D)
if preliminary:
	mainPlotter.draw(prelim)	

""" Modifies the JaAxes and the axes labels"""
labX = mainPlotter.getLabelX()[0][0]
labX.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(0, labX, mainPlotter, 0.025)
labY = mainPlotter.getLabelY()[0][0]
labY.setFont(axisLabelFont)
HPlotJaUtil.setLabelLocationAtCenter(1, labY, mainPlotter, -0.01)

theAxes_11 = mainPlotter.getPad()
theAxes_11.setLabelSpace(0, 0.0)
#theAxes_11.setLabelSpace(1, -0.025)
theAxes_11.setLabelSpace(1, 0.0)
HPlotJaUtil.setTickLabelOneToPower(0, theAxes_11)
HPlotJaUtil.setTickLabelZeroPositive(theAxes_11)

mainPlotter.update()

""" Saves or not the plot"""
outputFile = "LY_T_ZnWO4_Prelim"
if writeFigures:
	for i in range(len(figType)):
		outFileName = outputFile + figType[i]
		mainPlotter.export(outPath + File.separator + outFileName)
		print("Writing File : " + outPath + File.separator + outFileName)
		
#mainPlotter.update()
print("That's all folks !")

